/**
 *
 * 版权所有 © cybersky.com
 *
 */
package cn.cybertron.primus.report.controller;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.view.ForwardView;

import cn.cybertron.nutz.ext.i18n.Bundle;
import cn.cybertron.nutz.ext.mvc.Pager;
import cn.cybertron.nutz.ext.mvc.adaptor.ExtendPairAdaptor;
import cn.cybertron.primus.logging.annotation.Log;
import cn.cybertron.primus.report.cache.ReportCacheManager;
import cn.cybertron.primus.report.custom.CustomStepItem;
import cn.cybertron.primus.report.custom.ReportCustomConfig;
import cn.cybertron.primus.report.custom.ReportCustomHelper;
import cn.cybertron.primus.report.custom.ReportCustomItem;
import cn.cybertron.primus.report.data.ZReportItem;
import cn.cybertron.primus.report.entity.ReportCache;
import cn.cybertron.primus.report.entity.ReportObject;
import cn.cybertron.primus.security.annotation.AccessRestrict;

/**
 * 用户自定义报表
 * 
 * @author ch
 *
 */
@IocBean
@InjectName("reportCustomController")
@At("/report/custom")
@AccessRestrict(permissions = "report")
public class ReportCustomController {

	protected Bundle bundle = new Bundle("report");

	/**
	 * 进入自定义报表添加页面
	 * @param request
	 * @param session
	 */
	@At("/")
	@Ok("jsp:pages.report.main")
	@Log(record = false)
	public void index(HttpServletRequest request, HttpSession session) {
		String[] customtypes = ReportCustomConfig.me().getSupportedCustomTypes();
		request.setAttribute("supported_custom_types", customtypes);
	}
	
	/**
	 * 选择报表类型获取添加步骤
	 * @param type
	 * @param request
	 * @param session
	 */
	@At("/start")
	@Ok("jsp:pages.report.custom.custom")
	@Log(key = "report.log.start.addcustomreport", bundle = "report")
	public void viewStart(@Param("type") String type, HttpServletRequest request, HttpSession session) {
		ReportCustomItem item = ReportCustomConfig.me().getCustomItem(type);
		if(item != null) {
			List<CustomStepItem> steps = item.getSteps(); 
			request.setAttribute("steps", steps);
			
			String css = item.getCss();
			if(!Strings.isEmpty(css)) {
				request.setAttribute("customCss", css.split(","));
			}

			String js = item.getJs();
			if(!Strings.isEmpty(js)) {
				request.setAttribute("customJs", js.split(","));
			}
		}
		
	}
	
	/**
	 * 获取下一步页面
	 * @param type
	 * @param id
	 * @param stepNum
	 * @param request
	 * @param ioc
	 * @return
	 */
	@At("/getStepContent")
    // @Ok("jsp:/${obj}")
	@Log(key = "report.log.get.stepcontent", bundle = "report")
	public View getStepContent(@Param("type") String type, @Param("step_number") int stepNum, HttpServletRequest request, Ioc ioc) {
		ReportCustomItem typeItem = ReportCustomConfig.me().getCustomItem(type);
		ReportCustomHelper helper = typeItem.getHelper();
		// 添加报表基本信息,  @Param("::report.") ReportObject report
		
		ReportObject report = null;
		if(report == null) {
			String cacheid = request.getParameter("report.cacheid");
			if(cacheid != null) {
				report = new ReportObject();
				report.setCacheid(cacheid);
			}
		}
		/*report = ReportManager.getInstance().getReport(report.getUuid());
		else if(report != null && report.getUuid() != null){
		}*/
		//为该步骤页面准备数据
		helper.prepareData(stepNum+1, report, request, ioc);
		
		List<CustomStepItem> steps = typeItem.getSteps();
		CustomStepItem stepItem = steps.get(stepNum);
		String url = stepItem.getUrl();
		//跳转到该步骤页面
		View view = new ForwardView(url);
		return view;
	}

	/**
	 * 获取中间表数据源
	 * @param type
	 * @param id
	 * @param stepNum
	 * @param request
	 */
	@At("/getDataSource")
	@Ok("json")
	@Log(key = "report.log.get.datasource", bundle = "report")
	public String getDataSource(@Param("uuid") String uuid, HttpServletRequest request) {
		StringBuffer filterfield = new StringBuffer();
		ReportCache cache = ReportCacheManager.me().getCacheByUuid(uuid);
		Vector<ZReportItem> groupitems = cache.getGroupitems();
		if(!Lang.isEmpty(groupitems))
		{
			for(ZReportItem item: groupitems){
				filterfield.append(item.getColumn()).append(",");
			}
			// 去掉最后的 ,
		}
		
		return filterfield.toString();
	}
	
	/**
	 * 添加自定义报表
	 * @param request
	 * @param session
	 */
	@At("/savereport")
	@Ok("json")
	@Fail("json")
	@Log(key = "report.log.save.customreport", bundle = "report")
	@AdaptBy(type= ExtendPairAdaptor.class)
	public void saveReport(@Param("::report.") ReportObject report, @Param("map:tableTitle")Map<String, String> tableTitle, HttpServletRequest request) 
	{
		
	}

	/**
	 * 根据条件模糊查找报表
	 * @param fuzzy
	 * @return
	 */
	@At("/listreport")
	@Ok("json")
	@Fail("json")
	@Log(key = "report.log.list.report", bundle = "report")
	public Pager<ReportObject> listReport(HttpSession session, @Param("fuzzy") String fuzzy , @Param("::pager.")Pager<ReportObject> pager){
		
		return pager;
	}
	
}

